package com.ict.ms.trackPlayBack.facade.common;

import com.ict.ms.trackPlayBack.facade.dto.Circular;
import com.ict.ms.trackPlayBack.facade.dto.PointH;

/**
 * Created by chenlong on 2017/9/29.
 * Info:
 */
public class IFInArea {

    /**
     * 判断一个点是否在圆形区域内
     * @param lon 未除以600000的数据库里面拿出来的经度
     * @param lat 未除以600000的数据库里面拿出来的纬度
     * @param circular  圆形对象
     * @return
     */
    public static boolean ifInCircular(int lon,int lat,Circular circular){
        double d60Wlon;
        double d60Wlat;
        double rad=Math.PI/180;
        double lat1;
        double lat2;
        double a;
        double distance;
        int R=6371000;//地球半径

        d60Wlon=(double) lon/600000;
        d60Wlat=(double)lat/600000;

        PointH p1=new PointH(d60Wlon,d60Wlat);
        PointH cirp2=new PointH(circular.getCirlon(),circular.getCirlat());

        rad=Math.PI/180;
        lat1=p1.getLat()*rad;
        lat2=cirp2.getLat()*rad;

        a=Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos((cirp2.getLon()-p1.getLon())*rad);
        distance=R*Math.acos((Math.min(a,1)));

        //两点之间的距离小于半径即在圆形区域内
        if(distance<circular.getRidius()){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 判断点是否在多边形区域内方法(调杜一凡算法)
     * @param lon
     * @param lat
     * @param p
     * @return
     */
    public static boolean ifInPolygons(int lon, int lat, PolygonWithLongitudeCrossed180 p){
        double dlon =(double)lon/600000;
        double dlat =(double)lat/600000;
        boolean ifOk = p.containsPoint(dlon,dlat);
//        System.out.println("经度为："+dlon+",纬度为:"+dlat+"的点在多边形区域内？ "+isOk);
        return ifOk;
    }

    /**
     * 判断点是否在扇环形区域
     * @param lon
     * @param lat
     * @param p
     * @return
     */
    public static boolean ifInSectorAnnular(int lon, int lat,AnnularSectorWithLongitudeCrossed180 p){
        double dlon =(double)lon/600000;
        double dlat =(double)lat/600000;
        boolean ifOk = p.containsPoint(dlon,dlat);
        System.out.println("原始点目标(SQL)Lon："+lon+",Lat："+lat+",该点除以60W以后Lon："+dlon+",Lat："+dlat+"，判断是否在该扇环形区域的结果为"+ifOk);
        return ifOk;
    }

    /**
     * 判断点是否在扇形区域
     * @param lon
     * @param lat
     * @param p
     * @return
     */
    public static boolean ifInSectorCircular(int lon, int lat,CircularSectorWithLongitudeCrossed180 p){
        double dlon =(double)lon/600000;
        double dlat =(double)lat/600000;
        boolean ifOk = p.containsPoint(dlon,dlat);
        System.out.println("原始点目标(SQL)Lon："+lon+",Lat："+lat+",该点除以60W以后Lon："+dlon+",Lat："+dlat+"，判断是否在该扇形区域的结果为"+ifOk);
        return ifOk;
    }
}
